﻿using System.Drawing.Printing;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApp;

public class DemoEnv
{
    /// <summary>
    /// 操作系统信息
    /// </summary>
    /// <returns></returns>
    public void GetOperatingSystem(ref string operatingSystem,ref string osArchitecture)
    {
        try
        {
            using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select * from Win32_OperatingSystem"))
            {
                foreach (ManagementObject ManagementObject in managementObjectSearcher.Get())
                {
                    operatingSystem = Convert.ToString(ManagementObject["Caption"]);
                    osArchitecture = Convert.ToString(ManagementObject["OSArchitecture"]);
                    break;
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
    }
    
    #region 方式一 使用 ManagementObject 类获取
    /// <summary>
    /// 获取打印机
    /// </summary>
    public List<string> GetPrinters()
    {
        var printerDevices = new List<string>();
        try
        {
            using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
            foreach (var device in searcher.Get())
            {
                if (device["Name"] != null)
                {
                    printerDevices.Add(Convert.ToString(device["Name"]));
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return printerDevices;
    }
    #endregion、

    #region 方式二 使用 Printing 获取
    /// <summary>
    /// 获取打印机
    /// </summary>
    public List<string> GetInstalledPrinters()
    {
        var printerDevices = new List<string>();
        try
        {
            foreach (string printerName in PrinterSettings.InstalledPrinters)
            {
                if (!printerDevices.Contains(printerName))
                {
                    printerDevices.Add(printerName);
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return printerDevices;
    }
    #endregion
    
    
    /// <summary>
    /// 获取摄像头设备
    /// </summary>
    public List<string> GetCameras()
    {
        var cameraDevices = new List<string>();
        try
        {
            using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')");
            foreach (var device in searcher.Get())
            {
                if (device["Caption"] != null)
                {
                    cameraDevices.Add(Convert.ToString(device["Caption"]));
                }
            }
        }
        catch(Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return cameraDevices;
    }
    
    public class DiskDrive
    {
        public string SerialNumber { get; set; }
        public string Model { get; set; }
        public string Size { get; set; }
    }
    
    /// <summary>
    /// 硬盘信息
    /// </summary>
    public List<DiskDrive> GetDiskDrive()
    {
        var diskDevices = new List<DiskDrive>();
        try
        {
            using ManagementClass managementClass = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                DiskDrive diskDrive = new DiskDrive();
                diskDrive.SerialNumber = Convert.ToString(managementObject.Properties["SerialNumber"].Value);
                diskDrive.Model = Convert.ToString(managementObject.Properties["Model"].Value);
                diskDrive.Size = Convert.ToString(managementObject.Properties["Size"].Value);
                diskDevices.Add(diskDrive);
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return diskDevices;
    }
    
    public class BaseBoard
    {
        public string SerialNumber { get; set; }
        public string Product { get; set; }
        public string Version { get; set; }
        public string Manufacturer { get; set; }
    }
    
    /// <summary>
    /// 主板信息
    /// </summary>
    public List<BaseBoard> GetMainBoardInfo()
    {
        var baseBoardDevices = new List<BaseBoard>();
        try
        {
            using ManagementClass managementClass = new ManagementClass("Win32_BaseBoard");
            var managementObjectCollection = managementClass.GetInstances();
            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                var diskDrive = new BaseBoard
                {
                    SerialNumber = Convert.ToString(managementObject.Properties["SerialNumber"].Value),
                    Product = Convert.ToString(managementObject.Properties["Product"].Value),
                    Version = Convert.ToString(managementObject.Properties["Version"].Value),
                    Manufacturer = Convert.ToString(managementObject.Properties["Manufacturer"].Value)
                };
                baseBoardDevices.Add(diskDrive);
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return baseBoardDevices;
    }
    
    /// <summary>
    /// 获取网络地址
    /// </summary>
    /// <returns></returns>
    public List<string> GetIPAddress()
    {
        var iPAddressList = new List<string>();
        try
        {
            var allIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (var iPAddress in allIPAddresses)
            {
                if (iPAddress.AddressFamily.Equals(AddressFamily.InterNetwork))
                {
                    // IP4
                    iPAddressList.Add(iPAddress.ToString());
                    continue;
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return iPAddressList;
    }
    
    #region 方式一 使用 NetworkInterface 类获取
    public List<string> GetPhysicalAddress()
    {
        var physicalAddressList = new List<string>();
        try
        {
            var allNetworkInterface = NetworkInterface.GetAllNetworkInterfaces();
            var result = string.Empty;
            foreach (var networkInterface in allNetworkInterface)
            {
                result = networkInterface.GetPhysicalAddress().ToString();
                result = result.Insert(2, ":");
                result = result.Insert(5, ":");
                result = result.Insert(8, ":");
                result = result.Insert(11, ":");
                result = result.Insert(14, ":");
                physicalAddressList.Add(result);
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return physicalAddressList;
    }
    #endregion
    #region 方式二 使用 ManagementObject 类获取
    public static List<string> GetMacAddress()
    {
        var physicalAddressList = new List<string>();
        try
        {
            using var managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var managementObjectCollection = managementClass.GetInstances();
            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                if (managementObject["IPEnabled"] != null && managementObject["MacAddress"] != null)
                {
                    if ("true".Equals(Convert.ToString(managementObject["IPEnabled"]),StringComparison.OrdinalIgnoreCase))
                    {
                        physicalAddressList.Add(Convert.ToString(managementObject["MacAddress"]));
                    }
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return physicalAddressList;
    }
    #endregion
    
    /// <summary>
    /// 处理器信息
    /// </summary>
    /// <returns></returns>
    public string GetCPUInfo()
    {
        StringBuilder stringBuilder = new StringBuilder();
        try
        {
            using (ManagementObjectCollection.ManagementObjectEnumerator searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor").Get().GetEnumerator())
            {
                if (searcher.MoveNext())
                {
                    ManagementObject managementObject = (ManagementObject)searcher.Current;
                    stringBuilder.Append(managementObject.GetPropertyValue("Name").ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(managementObject.GetPropertyValue("CurrentClockSpeed").ToString());
                    stringBuilder.Append(" Mhz,");
                    stringBuilder.Append(Environment.ProcessorCount.ToString());
                    stringBuilder.Append("核心");
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return stringBuilder.ToString();
    }
}